package com.tlsy.commerce.utils;

import com.tlsy.commerce.dao.UserDao;
import com.tlsy.commerce.model.User;
import com.tlsy.commerce.shiro.CustomPrincipal;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

/**
 * shiro工具类
 * @author Tlsy
 * @version commerce 0.0.1
 * @date 2017/4/19  14:45
 */
public class ShiroUtil {

    private static UserDao userDao=SpringContextHolder.getBean(UserDao.class);

    /**
     * 获取shiro的subject对象
     *
     * @return
     */
    public static Subject getSubject() {
        return SecurityUtils.getSubject();
    }


    /**
     * 根据ID获取用户
     * @param id
     * @return 取不到返回null
     */
    public static User getCurrentUserById(String id){
        User user = (User)CacheUtil.get(AppConfig.USER_CACHE, AppConfig.USER_CACHE_ID_ + id);
        if (user ==  null){
            user = userDao.findOne(id);
            if (user == null){
                return null;
            }
            //user.setRoleList(roleDao.findList(new Role(user)));
            CacheUtil.put(AppConfig.USER_CACHE, AppConfig.USER_CACHE_ID_ + user.getId(), user);
            CacheUtil.put(AppConfig.USER_CACHE, AppConfig.USER_CACHE_NAME_ + user.getName(), user);
        }
        return user;
    }

    /**
     * 根据ID获取用户
     * @param phone
     * @return 取不到返回null
     */
    public static User getCurrentUserByPhone(String phone){
        User user = (User)CacheUtil.get(AppConfig.USER_CACHE, AppConfig.USER_CACHE_PHONE_ + phone);
        if (user ==  null){
            user = userDao.findUserByPhone(phone);
            if (user == null){
                return null;
            }
            //user.setRoleList(roleDao.findList(new Role(user)));
            CacheUtil.put(AppConfig.USER_CACHE, AppConfig.USER_CACHE_PHONE_ + user.getPhone(), user);
            CacheUtil.put(AppConfig.USER_CACHE, AppConfig.USER_CACHE_NAME_ + user.getName(), user);
        }
        return user;
    }

    /**
     * 获取当前登录者对象
     */
    public static CustomPrincipal getCustomPrincipal(){
        try{
            Subject subject = SecurityUtils.getSubject();
            CustomPrincipal principal = (CustomPrincipal)subject.getPrincipal();
            if (principal != null){
                return principal;
            }
//			subject.logout();
        }catch (UnavailableSecurityManagerException e) {

        }catch (InvalidSessionException e){

        }
        return null;
    }

    public static Session getSession(){
        try{
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession(false);
            if (session == null){
                session = subject.getSession();
            }
            if (session != null){
                return session;
            }
//			subject.logout();
        }catch (InvalidSessionException e){

        }
        return null;
    }

    /**
     * shiro退出当前用户登录
     */
    public static void loginOut() {
         getSubject().logout();
    }
}
